/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itttq.gshop.domain.po.UserAddressPo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.util.Constants;
import com.itttq.gshop.pbpm.mapper.IUserAddressMapper;

@Service
public class UserAddressService {
	@Autowired
	private IUserAddressMapper userAddressMapper;
	
	/**
	 * 根据ID获取UserAddressPo对象
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return UserAddressPo
	 */
	public UserAddressPo getUserAddress(int id) {
		return userAddressMapper.findUserAddressById(id);
	}
	
	/**
	 * 根据条件获取UserAddressPo对象，并返回查询的数据总数
	 * 
	 * @param userAddress
	 * @Author 迷蝶
	 * @return ResultPageTo<UserAddressPo>
	 */
	public ResultPageTo<UserAddressPo> getUserAddresss(UserAddressPo userAddress) {
		return new ResultPageTo<UserAddressPo>(userAddress.getStart(), userAddress.getSize(), 
				userAddressMapper.findUserAddressCount(userAddress), userAddressMapper.findUserAddresss(userAddress));
	}
	
	/**
	 * 根据ID删除UserAddressPo对象
	 * 
	 * @param id
	 * @Author 迷蝶
	 */
	public Integer delUserAddress(int id) {
		return userAddressMapper.delUserAddressById(id);
	}
	
	/**
	 * 增加UserAddressPo对象
	 * 
	 * @param userAddress
	 * @Author 迷蝶
	 */
	public Integer addUserAddress(UserAddressPo userAddress) {
		return userAddressMapper.addUserAddress(userAddress);
	}
	
	/**
	 * 根据条件更新UserAddressPo对象
	 * 
	 * @param userAddress
	 * @Author 迷蝶
	 */
	public Integer updateUserAddress(UserAddressPo userAddress) {
		return userAddressMapper.updateUserAddress(userAddress);
	}
	
	/**
	 * 保存或新增用户地址，ID存在则更新，反之则新增
	 * 
	 * @param userAddress
	 * @Author 迷蝶
	 */
	public Integer saveUserAddress(UserAddressPo userAddress) {
		int rs = 0;
		if(null == userAddress) {
			return rs;
		}
		// 处理默认地址，如果设置为默认地址，则其他默认地址更新为非默认地址
		if(userAddress.getDefaultAddr().intValue() == Constants.STATUTS_OPEN) {
			userAddressMapper.updateDefaultAddr(userAddress.getUserId());
		}
		// 判断是不是设置默认地址，有则取消之前的默认地址
		if(null != userAddress.getDefaultAddr() && userAddress.getDefaultAddr().intValue() == Constants.STATUTS_OPEN) {
			UserAddressPo defaultAddr = getDefaultAddr(userAddress.getUserId());
			if(null != defaultAddr) {
				defaultAddr.setDefaultAddr(Constants.STATUTS_CLOSE);
				updateUserAddress(defaultAddr);
			}
		}
		if(null == userAddress.getId()) {
			rs = addUserAddress(userAddress);
		} else {
			rs = updateUserAddress(userAddress);
		}
		return rs;
	}
	
	/**
	 * 根据用户ID获取默认地址
	 * 
	 * @param userId
	 * @Author 迷蝶
	 */
	public UserAddressPo getDefaultAddr(Integer userId) {
		UserAddressPo addr = new UserAddressPo();
		addr.setUserId(userId);
		addr.setDefaultAddr(Constants.STATUTS_OPEN);
		List<UserAddressPo> addrs = userAddressMapper.findUserAddresss(addr);
		return null == addrs || addrs.isEmpty() ? null : addrs.get(0);
	}
	
}
